package br.ufmg.dcc.simulesspl.aspect;
import br.ufmg.dcc.simulesspl.states.*;
import java.io.IOException;

public aspect Sharing {
	
	
	public pointcut fimDoJogo():
		call(void EndState.run())
		;
	
	after() : fimDoJogo()
	{
		byte[] b = new byte[30];
		System.out.println("Quer dividir isso com o mundo? s/n");
		try
		{
			System.in.read(b);
		}
		catch(IOException e)
		{
			xingar();
			return;
		}
		String s = new String(b);
		if(s.compareToIgnoreCase("s")==0)
		{
			try
			{
				System.in.read(b);
			}
			catch(IOException e)
			{
				xingar();
				return;
			}
			s = new String(b);
			if(s.compareToIgnoreCase("facebook")==0)
			{
				openBrowser("www.twitter.com");
			}
			else
			{
				openBrowser("www.facebook.com");
			}
			return;
		}
		xingar();
		return;
	}
	
	public void openBrowser(String url) {

        if( !java.awt.Desktop.isDesktopSupported() ) {

            System.out.println( "Desktop is not supported (fatal)" );
            return;
        }

        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

        if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {

            System.out.println( "Desktop doesn't support the browse action (fatal)" );
            return;
        }

        try {

            java.net.URI uri = new java.net.URI(url);
            desktop.browse( uri );
        }
        catch ( Exception e ) {

            System.out.println( e.getMessage() );
        }
    }
	
	public void xingar()
	{
		System.out.println("Você é muito egoísta. Tanta diversão, só para você?");
	}
}
